# coding=utf-8
import redis, datetime
from MySqlConn import Mysql

pool = redis.ConnectionPool(host='127.0.0.1', port='6379', db=3)
redis_save = redis.Redis(connection_pool=pool)


def total_money(storeId, machineId, price):
    try:
        day = datetime.datetime.today().strftime('%Y-%m-%d')
        mysql = Mysql()
        sql = "select * from car_total where day='%s' and storeId='%s' and machineId='%s' " % (day, storeId, machineId)
        result = mysql.getOne(sql, None)
        if result:
            sql1 = "update car_total set money=money+'%s' where day='%s' and storeId='%s' and machineId='%s'" % (
                (float(price) / 100), day, storeId, machineId)
            mysql.update(sql1, None)
        else:
            sql1 = "insert into  car_total(day,storeId,machineId,money) values('%s','%s','%s','%s')" % (
                day, storeId, machineId, (float(price) / 100))
            mysql.insertOne(sql1, None)
        mysql.dispose()
    except Exception as e:
        print e


def update_channel():
    key = "machine_pay_" + datetime.datetime.today().strftime('%Y-%m-%d')
    data_all = redis_save.lpop(key)
    if data_all is None:
        return False
    mysql = Mysql()
    try:
        data = eval(data_all)
        machine_id = data['machineId']
        store_id = data['store_id']
        uid = data['uid']
        order_time = data['order_time']
        price = data['money']
        pay_time = data['pay_time']
        wx_order = data['wx_order']
        good_order = data['good_order']
        plateNum = data['plateNum']
        flag = data['flag']
        mysql = Mysql()
        if int(flag) == 0:
            insertSql = "insert into car_orders(orderNum,payOrderNum,orderTime," \
                        "payTime,uid,machineId,storeId,price,plateNum,status,orderType)" \
                        "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s',1,0)" % (
                            good_order, wx_order, order_time, pay_time, uid, machine_id, store_id,
                            float(price) / 100, plateNum)
            mysql.insertOne(insertSql, None)
        elif int(flag) == -1:
            result = mysql.getOne("select * from car_machine where machineId='%s'" % machine_id, None)
            store_id = result['storeId']
            insertSql = "insert into car_orders(orderNum,payOrderNum,orderTime," \
                        "payTime,uid,machineId,storeId,price,plateNum,status,orderType)" \
                        "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s',1,0)" % (
                            good_order, wx_order, order_time, pay_time, uid, machine_id, result['storeId'],
                            float(price) / 100, plateNum)
            mysql.insertOne(insertSql, None)
        else:
            sql = "select * from car_users where uid='%s' " % uid
            result = mysql.getOne(sql, None)
            if result:
                sql1 = "select * from recharge where id='%s'" % flag
                res = mysql.getOne(sql1, None)
                userSql = "update car_users set washNum=washNum+'%s',plateNum='%s' where uid='%s'" % (
                    int(res['num']), plateNum, uid)
                mysql.update(userSql, None)
                insertSql = "insert into car_orders(orderNum,payOrderNum,orderTime," \
                            "payTime,uid,machineId,storeId,price,plateNum,status)" \
                            "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s',1)" % (
                                good_order, wx_order, order_time, pay_time, uid, machine_id, store_id,
                                float(price) / 100, plateNum)
                mysql.insertOne(insertSql, None)
        total_money(store_id, machine_id, price)
        mysql.dispose()
        return True
    except Exception as e:
        mysql.dispose()
        print e
